
import axios from 'axios'
import {baseUrl} from '../config/env'

const data = localStorage.userData ? localStorage.userData : '{}'
const userData = JSON.parse(data)
const Token = userData.user && userData.user.token || 'TOKEN'

// 创建axios实例
const service = axios.create({
  baseURL: baseUrl, // api的base_url
  timeout: 10000    // 请求超时时间
})

// request拦截器
service.interceptors.request.use(config => {
  config.headers['token'] = Token;
  config.headers['Content-Type'] = 'application/json;charset=UTF-8';
  return config
}, error => {
  Promise.reject(error)
})

// respone拦截器
service.interceptors.response.use(
  response => response.data,
  error => {
    console.log('err' + error)
    return Promise.reject(error)
  }
)

export default service
